﻿#region System Namespace

using System.Collections.Generic;
using System.Diagnostics;
using System;
using System.Linq;
using Styx;
using Styx.Common;
using Styx.CommonBot.Frames;
using Styx.Plugins;
using Styx.WoWInternals;

    #endregion System Namespace

#region Foreign Namespace
#endregion Foreign Namespace

#region Styx Namespace

#endregion Styx Namespace

namespace F2FTraderq
{
    public class Shopkeeper : HBPlugin
    {
        public Stopwatch Sw = new Stopwatch();
        public Stopwatch Sw2 = new Stopwatch();
        public override void Pulse()
        {
            if (StyxWoW.IsInGame && MIRsettings.Instance.CharacterName == StyxWoW.Me.Name)
            {
                if (StyxWoW.Me.IsAlive)
                {
                    if (MIRsettings.Instance.BtnAdvertise)
                    {
                        OutsideAdvertisePulse();
                    }
                    if (MIRsettings.Instance.Cod)
                    {
                        OutsideCodPulse();
                    }
                }
            }
        }
        public void OutsideAdvertisePulse()
        {
            Sw.Start();
            if (!MIRsettings.Instance.CustomMessage)
            {
                if (Sw.IsRunning)
                {
                    if (MIRsettings.Instance.PostMessageTimer != 0 &&
                        Sw.IsRunning &&
                        Sw.Elapsed.Minutes > MIRsettings.Instance.PostMessageTimer +
                        TimeSpanUtil.ConvertMillisecondsToMinutes(new Random().Next(5000, 25000)))

                        Lua.DoString("SendChatMessage(\"WTS \"..select(2,GetItemInfo(" +
                                     new Main().RemoveDigits(MIRsettings.Instance.SelectedItem) + "))..\"" + " " +
                                     MIRsettings.Instance.SendMessage + "\",\"channel\",nil,2)");
                    Sw.Reset();
                    Sw.Stop();
                }
            }
            else
            {
                Sw2.Start();
                if (Sw2.IsRunning)
                {
                    if (MIRsettings.Instance.PostMessageTimer != 0 &&
                        Sw2.IsRunning &&
                        Sw2.Elapsed.Minutes > MIRsettings.Instance.PostMessageTimer +
                        TimeSpanUtil.ConvertMillisecondsToMinutes(new Random().Next(5000, 25000)))
                    {
                        new Main().Log("[Automatic]: Posting to Trade... " + DateTime.Now.ToLocalTime());
                        Lua.DoString("SendChatMessage(\"" + MIRsettings.Instance.SendMessage + "\",\"channel\",nil,2)");
                        Sw2.Reset();
                        Sw2.Stop();
                    }
                }
            }
        }
        public void OutsideCodPulse()
        {
            if (new MailFrame().IsVisible)
            {
                if (StyxWoW.Me.BagItems.Count > 60)
                    new Main().SendMail();
                else
                    new Main().OpenAttachments();
            }
            else if (!new MailFrame().IsVisible)
            {
                new Main().MoveToMailbox();
                new Main().InteractWithMailbox();
            }
        }

        public IEnumerable<MailFrame.InboxMailItem> GetAllMail()
        {
            return new MailFrame().GetAllMails().ToList();
        }

        public override void OnButtonPress() { new F2FTraderForm().ShowDialog(); }
        public override string Name { get { return "Shopkeeper"; } }
        public override string Author { get { return "Giwin"; } }
        public override Version Version { get { return new Version(1, 0); } }
        public override bool WantButton { get { return true; } }

        public override void Initialize()
        {
            Lua.Events.AttachEvent("MAIL_SHOW", (sender, args) => new Main().MailboxOpen());
            Lua.Events.AttachEvent("MAIL_CLOSED", (sender, args) => new Main().MailboxClosed());
            Lua.Events.AttachEvent("TRADE_MONEY_CHANGED", (sender, args) => new Main().TradeMoneyMatchesSetRequirement());
            Lua.Events.AttachEvent("TRADE_MONEY_CHANGED", (sender, args) => new Main().MoneyOfferedChanged());
            new Main().Log("Loaded");
        }
    }
}